package nz.co.doherty.gwtpbreadcrumbs.client.ui.customers;

import nz.co.doherty.gwtpbreadcrumbs.client.NameTokens;
import nz.co.doherty.gwtpbreadcrumbs.client.gin.ClientGinjector;
import nz.co.doherty.gwtpbreadcrumbs.client.i8n.ClientConstants;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.app.AppPresenter;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.common.ListItemFactory;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.common.ListPresenter;
import nz.co.doherty.gwtpbreadcrumbs.shared.entity.Customer;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.customer.CustomerListAction;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.customer.CustomerListResult;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.dispatch.client.DispatchAsync;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyCodeSplit;
import com.philbeaudoin.gwtp.mvp.client.annotations.TitleFunction;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceManager;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceRequest;
import com.philbeaudoin.gwtp.mvp.client.proxy.ProxyFailureHandler;
import com.philbeaudoin.gwtp.mvp.client.proxy.ProxyPlace;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealContentEvent;

public class CustomerListPresenter
		extends
		ListPresenter<CustomerListPresenter.CustomerListProxy, Customer, CustomerListItemView, CustomerListItemPresenter>
		implements CustomerListView.Presenter {

	private final DispatchAsync dispatcher;

	@Inject
	public CustomerListPresenter(
			EventBus eventBus,
			CustomerListView view,
			ListItemFactory<Customer, CustomerListItemView, CustomerListItemPresenter> itemFactory,
			CustomerListProxy proxy, ProxyFailureHandler failureHandler,
			PlaceManager placeManager, ClientConstants constants,
			DispatchAsync dispatcher) {
		super(eventBus, view, itemFactory, proxy, failureHandler, placeManager,
				constants);
		this.dispatcher = dispatcher;
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.customerList)
	public interface CustomerListProxy extends ProxyPlace<CustomerListPresenter> {
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(eventBus, AppPresenter.TYPE_RevealMainContent,
			this);
	}

	@TitleFunction
	public static String getPageTitle(ClientGinjector ginjector) {
		return ginjector.getConstants().listCustomers();
	}

	@Override
	protected void onReset() {
		super.onReset();
	}

	@Override
	protected void onReveal() {
		super.onReveal();

		dispatcher.execute(new CustomerListAction(),
			new AsyncCallback<CustomerListResult>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(CustomerListResult result) {
					// TODO Auto-generated method stub
					CustomerListPresenter.this.setListData(result.getCustomers());
				}
			});
	}

	@Override
	public void onCreate() {
		placeManager.revealRelativePlace(new PlaceRequest(
				NameTokens.getCustomerCreate()));
	}
}
